package com.jd.basic.service.impl;

import com.jd.basic.dto.SmsCodeDto;
import com.jd.basic.exception.BusinessException;
import com.jd.basic.service.IVerifyCodeService;
import com.jd.basic.util.StrUtils;
import com.jd.basic.util.VerifyCodeUtils;
import com.jd.user.domain.User;
import com.jd.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String image (String key) {
        //使用工具类生成图形验证码
        String code = StrUtils.getComplexRandomString(4);//1213
        //保存到redis
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        //使用工具类生成图形验证码编码之后的base64的字符串
        String base64Str = VerifyCodeUtils.verifyCode(110, 40, code);
        return base64Str;

    }

    @Override
    public void smsCode(SmsCodeDto smsCodeDto) {
        // 1.校验-空值校验
        String imageCode = smsCodeDto.getImageCode();
        String phone = smsCodeDto.getPhone();
        String key = smsCodeDto.getImageCodeKey();
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号码不能够为空");
        }
        if (StringUtils.isEmpty(imageCode)){
            throw new BusinessException("图形验证码不能够为空");
        }

        // 2.校验-图形验证码是否过期
        Object o = redisTemplate.opsForValue().get(key);
        if (o == null){
            throw new BusinessException("图形验证码过期，请重新验证");
        }
        if (!o.toString().equalsIgnoreCase(imageCode)){
            throw new BusinessException("图形验证码错误");
        }
        // 3.校验-手机号码是否注册
        User user = userMapper.loadByPhone(phone);
        if (user!= null){
            throw new BusinessException("手机号码已经注册，请直接登录");
        }

        // 4.获取手机验证码判断是否过期
        Object codeObj = redisTemplate.opsForValue().get("register:" + phone);
        // 4.1 过期了或者没有
        String smsCode = null;
        if (codeObj==null){
            smsCode = StrUtils.getRandomString(4);
        }else {
            long oldTime = Long.parseLong(codeObj.toString().split(":")[1]);

            if ((System.currentTimeMillis()-oldTime)<60*1000){
                throw new BusinessException("请勿频繁操作");
            }

            smsCode = codeObj.toString().split(":")[0];

        }
        //4.2保存到redis - 重新设置3分钟过期
        redisTemplate.opsForValue().set("register:"+phone,smsCode+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);


        //4.3调用工具类发送短信 - sout作弊
        System.out.println("你的验证码为"+smsCode+",请你在3分钟之内完成");


    }




    @Override
    public void binderSmsCode(SmsCodeDto smsCodeDto) {
        // 1.校验-空值校验
        String phone = smsCodeDto.getPhone();
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号码不能够为空");
        }


        // 4.获取手机验证码判断是否过期
        Object codeObj = redisTemplate.opsForValue().get("binder:" + phone);
        // 4.1 过期了或者没有
        String smsCode = null;
        if (codeObj==null){
            smsCode = StrUtils.getRandomString(4);
        }else {
            long oldTime = Long.parseLong(codeObj.toString().split(":")[1]);

            if ((System.currentTimeMillis()-oldTime)<60*1000){
                throw new BusinessException("请勿频繁操作");
            }

            smsCode = codeObj.toString().split(":")[0];

        }
        //4.2保存到redis - 重新设置3分钟过期
        redisTemplate.opsForValue().set("binder:"+phone,smsCode+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);


        //4.3调用工具类发送短信 - sout作弊
        System.out.println("你的验证码为"+smsCode+",请你在3分钟之内完成");


    }




}
